package cn.com.shopec.erp.financing.commo;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import cn.com.shopec.erp.financing.entity.FreightSettlementDetail;
import cn.com.shopec.erp.financing.vo.FreightSettlementVo;

public class FreightSettlementExport {
	private static Logger log = Logger.getLogger(DataExport.class);
	/**工作簿**/
	private HSSFWorkbook workbook = null;
	/**工作表**/
	private HSSFSheet sheet = null;
	/**工作行**/
	private HSSFRow row = null;
	/**单元 格**/
	private HSSFCell cell = null;
	private static final int SHEET_MAX_COUNT = 65535;


	/**
	 * 创建标题
	 * @param row
	 * @param title
	 */
	@SuppressWarnings("deprecation")
	public void createTitle(HSSFRow row , String [] title)
	{
		try
		{
			cell = row.createCell((short) 0);   
			
			for (int i = 0 ; i < title.length ; i++)
			{
				  cell = row.createCell((short) i);   
			      cell.setCellValue(title[i]);   
			}
		}
		catch(Exception e)
		{
			log.error("DataExport writeTitle excpeion:",e);
		}
		
	}
	
	
	/**
	 * 
	 * @param row
	 * @param title
	 */
	public void createWrite(OutputStream out )
	{
		try 
		{
			workbook.write(out);
		} 
		catch (FileNotFoundException e) 
		{
			log.error("DataExport createWrite FileNotFoundException:",e);
		}  
		catch (IOException e) 
		{
			log.error("DataExport createWrite IOException:",e);
		}
		finally
		{
			if(out != null)
			{
				try 
				{
					out.flush();
					out.close();
				} 
				catch (IOException e) 
				{
					log.error("DataExport createWrite IOException:",e);
				}
			}
		}
	
		
	}
	

	/**
	 * 创建联系人sheet
	 * @param fsVoList
	 * @param title
	 * @param out
	 */
	@SuppressWarnings("deprecation")
	public void createNumberSheet(List<FreightSettlementVo> fsVoList ,
			List<FreightSettlementDetail> nowlist,
			List<FreightSettlementDetail> beforelist,
								 String [] title , OutputStream out) 
	{
		
	    //默认为1
		FreightSettlementVo vo = fsVoList.get(0);
        int sheetNum = 1;
        int totalSize=fsVoList.size()+nowlist.size()+beforelist.size()+20;
        int bef = beforelist.size();
        int ben = nowlist.size();
        //计算要创建的sheet个数
        if(totalSize % SHEET_MAX_COUNT > 0)
        {
            sheetNum = fsVoList.size() / SHEET_MAX_COUNT + 1;
        }
        else
        {
            sheetNum = fsVoList.size() / SHEET_MAX_COUNT;
        } 
        
       
         workbook = new HSSFWorkbook();  
         
		
		 for(int i = 0; i < sheetNum ; i++)
		 {
			//i ----------------工作表数量
		    sheet = workbook.createSheet(("sheet"+i)); 
	        row = sheet.createRow(0);   
	           
	        createTitle(row , title);
	         // 循环填写第行数据     
	        for(int j = i * SHEET_MAX_COUNT ; j < (i+1) * SHEET_MAX_COUNT ; j++ )
	        {
                if(j < totalSize)
                {	
                	
                	if(j<=7){
                	
                	//填写结算单表头信息信息                	
                		if(j==0){
                			row = sheet.createRow(j+1 - (i*SHEET_MAX_COUNT)); 
                			cell = row.createCell((short) 3);
                			cell.setCellValue("运费结算清单");
                			continue;
                		}
                		if(j ==1 ){
                			row = sheet.createRow(j+1 - (i*SHEET_MAX_COUNT)); 
                			cell = row.createCell((short) 0);
                			cell.setCellValue("运费结算单号");
                			cell = row.createCell((short) 1);
                			cell.setCellValue(vo.getStatementNo());
                			cell = row.createCell((short) 2);
                			cell.setCellValue("物流公司");
                			cell = row.createCell((short) 3);
                			cell.setCellValue(vo.getLogisticsName());
                			cell = row.createCell((short) 4);
                			cell.setCellValue("物流公司编码");
                			cell = row.createCell((short) 5);
                			cell.setCellValue(vo.getLogisticsNo());
                			continue;
                		}
                		if(j ==2 ){
                			row = sheet.createRow(j+1 - (i*SHEET_MAX_COUNT)); 
                			cell = row.createCell((short) 0);
                			cell.setCellValue("结算金额");
                			cell = row.createCell((short) 1);
                			cell.setCellValue(vo.getStatementTotal());
                			cell = row.createCell((short) 2);
                			cell.setCellValue("结算开始日期");
                			cell = row.createCell((short) 3);
                			cell.setCellValue(vo.getStartDate());
                			cell = row.createCell((short) 4);
                			cell.setCellValue("结算结束日期");
                			cell = row.createCell((short) 5);
                			cell.setCellValue(vo.getEndDate());
                			continue;
                		}
                		if(j ==3 ){
                			row = sheet.createRow(j+1 - (i*SHEET_MAX_COUNT)); 
                			cell = row.createCell((short) 0);
                			cell.setCellValue("结算人");
                			cell = row.createCell((short) 1);
                			cell.setCellValue(vo.getStatementName());
                			cell = row.createCell((short) 2);
                			cell.setCellValue("审核状态");
                			cell = row.createCell((short) 3);
                			cell.setCellValue(vo.getCheckStateName());
                			cell = row.createCell((short) 4);
                			cell.setCellValue("结算状态");
                			cell = row.createCell((short) 5);
                			cell.setCellValue(vo.getStatementStateName());
                			continue;
                		}
                		if(j == 4)
                		{
                			row = sheet.createRow(j+1 - (i*SHEET_MAX_COUNT)); 
                			cell = row.createCell((short) 0);
                			cell.setCellValue("实际结算金额");
                			cell = row.createCell((short) 1);
                			cell.setCellValue(vo.getActTotal());
                			continue;
                		}
                		
                		
                		/**本期之前表头信息**/
                		if(j ==6 ){
                			row = sheet.createRow(j+1 - (i*SHEET_MAX_COUNT)); 
                			cell = row.createCell((short) 0);
                			cell.setCellValue("运费结算单(本期之前)");           		
                			continue;
                		}
                		if(j ==7 ){
                			row = sheet.createRow(j+1 - (i*SHEET_MAX_COUNT)); 
                			cell = row.createCell((short) 0);
                			cell.setCellValue("快递单号"); 
                			cell = row.createCell((short) 1);
                			cell.setCellValue("发货日期");  
                			cell = row.createCell((short) 2);
                			cell.setCellValue("订单号");  
                			cell = row.createCell((short) 3);
                			cell.setCellValue("发货地址");  
                			cell = row.createCell((short) 4);
                			cell.setCellValue("商品重量(kg)");  
                			cell = row.createCell((short) 5);
                			cell.setCellValue("物流费用"); 
                			cell = row.createCell((short) 6);
                			cell.setCellValue("下单时间"); 
                			continue;
                		}
                		
                	}else{
                		if(j-8<bef){
                			
                			//本期之前
                			row = sheet.createRow(j+1 - (i*SHEET_MAX_COUNT)); 
            				cell = row.createCell((short) 0);
            				cell.setCellValue(beforelist.get(j-8).getExpressNo()); 
            				cell = row.createCell((short) 1);
            				cell.setCellValue(beforelist.get(j-8).getSendDate());
            				cell = row.createCell((short) 2);
            				cell.setCellValue(beforelist.get(j-8).getOrderNo()); 
            				cell = row.createCell((short) 3);
            				cell.setCellValue(beforelist.get(j-8).getSendAddress());  
            				cell = row.createCell((short) 4);
            				cell.setCellValue(beforelist.get(j-8).getProductWeight());  
            				cell = row.createCell((short) 5);
            				cell.setCellValue(beforelist.get(j-8).getLogisticsExpenses()); 
            				cell = row.createCell((short) 6);
            				cell.setCellValue(beforelist.get(j-8).getOrderTime());  
            				
            				continue;
                		}else if(j-(8+bef)<ben+2){
                			if((j-(8+bef))==0){
                				row = sheet.createRow(j+1 - (i*SHEET_MAX_COUNT)); 
                    			cell = row.createCell((short) 0);
                    			cell.setCellValue("运费结算单(本期)");           		
                    			continue;
                			}else if ((j-(8+bef))==1){
                				row = sheet.createRow(j+1 - (i*SHEET_MAX_COUNT)); 
                				cell = row.createCell((short) 0);
                    			cell.setCellValue("快递单号"); 
                    			cell = row.createCell((short) 1);
                    			cell.setCellValue("发货日期");  
                    			cell = row.createCell((short) 2);
                    			cell.setCellValue("订单号");  
                    			cell = row.createCell((short) 3);
                    			cell.setCellValue("发货地址");  
                    			cell = row.createCell((short) 4);
                    			cell.setCellValue("商品重量(kg)");  
                    			cell = row.createCell((short) 5);
                    			cell.setCellValue("物流费用"); 
                    			cell = row.createCell((short) 6);
                    			cell.setCellValue("下单时间"); 
                    			continue;
                			}else{
                			//本期                			
                				
                				row = sheet.createRow(j+1 - (i*SHEET_MAX_COUNT)); 
                				cell = row.createCell((short) 0);
                				cell.setCellValue(nowlist.get(j-(8+bef)-2).getExpressNo()); 
                				cell = row.createCell((short) 1);
                				cell.setCellValue(nowlist.get(j-(8+bef)-2).getSendDate());
                				cell = row.createCell((short) 2);
                				cell.setCellValue(nowlist.get(j-(8+bef)-2).getOrderNo()); 
                				cell = row.createCell((short) 3);
                				cell.setCellValue(nowlist.get(j-(8+bef)-2).getSendAddress());  
                				cell = row.createCell((short) 4);
                				cell.setCellValue(nowlist.get(j-(8+bef)-2).getProductWeight());  
                				cell = row.createCell((short) 5);
                				cell.setCellValue(nowlist.get(j-(8+bef)-2).getLogisticsExpenses());  
                				cell = row.createCell((short) 6);
                				cell.setCellValue(beforelist.get(j-8).getOrderTime());  
                			}
                		}else{
                			break;
                		}
                	}
                }
                else
                {
                	break;
                }
	        }

		 }
           
        createWrite(out);
	}
}
